package com.smart.controller;


import com.smart.entity.MachineSituation;
import com.smart.entity.Result;
import com.smart.service.IMachineSituationService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 设备故障表 前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-05-20
 */
@RestController
@CrossOrigin
@RequestMapping("/machine-situation")
@Api(tags = "设备故障相关接口")
public class MachineSituationController {
    @Autowired
    private IMachineSituationService machineSituationService;
    @Autowired
    private HttpSession session;

    @GetMapping("/{current}/{size}")
    @ApiOperation(value = "分页获取设备故障列表", response = Result.class)
    public Result getMachineSituationList(
            @PathVariable @ApiParam(name = "当前页", required = true) Integer current,
            @PathVariable @ApiParam(name = "每页数量", required = true) Integer size,
            @RequestParam(required = false) @ApiParam(name = "设备编号") String deviceId) {
        if (current == null || size == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(machineSituationService.getMachineSituationList(current, size, deviceId));
    }

    @PostMapping
    @ApiOperation(value = "添加设备故障", response = Result.class)
    public Result addMachineSituation(
            @RequestBody @ApiParam(name = "设备故障信息", required = true) MachineSituation machineSituation) {
        if (machineSituation == null) {
            return Result.error("参数不能为空");
        }
        Claims claims = (Claims) session.getAttribute("claims");
        machineSituation.setCommitPerson((String) claims.get("name"));
        boolean isSucceed = machineSituationService.save(machineSituation);
        return isSucceed ? Result.success() : Result.error("操作失败");
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改指定设备故障状态", response = Result.class)
    public Result updateMachineSituation(
            @PathVariable @ApiParam(name = "设备故障ID", required = true) Integer id,
            @RequestBody @ApiParam(name = "设备故障处理信息 0-未处理 1-已处理", required = true) Integer status) {
        MachineSituation machineSituation = new MachineSituation();
        machineSituation.setId(id);
        machineSituation.setStatus(status);
        boolean isSucceed = machineSituationService.updateById(machineSituation);
        return isSucceed ? Result.success() : Result.error("操作失败");
    }
}
